//1.滑动窗口---总和>t的长度最小的子数组
//双指针暴力解法 -> 发现两个指针可以都不回退 -> 滑动窗口
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size(), l = 0, r = 0;
        int sum = 0, ret = 0x3f3f3f3f;
        while(r < n)
        {
            sum += nums[r]; //进窗口
            while(sum >= target) //判断是否合法
            {
                ret = min(ret, r - l + 1); //更新结果
                sum -= nums[l++]; //出窗口
            }
            r++;
        }
        return ret == 0x3f3f3f3f ? 0 : ret;
    }
};


//2.滑动窗口---无重复字符的最长子串
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int hash[128]; 
        int n = s.size(), ret = 0;
        int l = 0, r = 0;
        while(r < n)
        {
            hash[s[r]]++; //进窗口
            while(hash[s[r]] > 1)
            {
                hash[s[l]]--;
                l++;
            }   
            ret = max(ret, r - l + 1);
            r++;
        }
        return ret;
    }
};